﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.Command;
using CPETwit.WPFClient.Common;

namespace CPETwit.WPFClient.ViewModel
{
    public class GlobalViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        private ICommand _openCreatorProfileCommand;
        private ICommand _followCommand;
        private readonly WCFServiceCaller _serviceCaller;
        private ICommand _openTwitterProfileCommand;

        public ICommand OpenCreatorProfileCommand
        {
            get
            {
                return _openCreatorProfileCommand ??
                    new RelayCommand(param => OpenProfile(param),
                                     param => CanOpenProfile());
            }
        }
        public ICommand OpenTwitterProfileCommand
        {
            get
            {
                return _openTwitterProfileCommand ??
                    new RelayCommand(param => OpenTwitterProfile(param),
                                     param => CanTwitterOpenProfile());
            }
        }

        public ICommand FollowCommand
        {
            get
            {
                return _followCommand ??
                    new RelayCommand(param => FollowUser(param as User),
                                     param => CanFollowUser());
            }
        }


        public GlobalViewModel()
        {
            _serviceCaller = CommonData.GetServiceCaller();
        }

        private bool CanOpenProfile()
        {
            return true;
        }
        private void OpenProfile(Object obj)
        {
            if (obj is Tweet)
                DialogManager.ShowProfileMenu((obj as Tweet).CreatorProperty);
            if (obj is User)
                DialogManager.ShowProfileMenu(obj as User);
        }
        private void OpenTwitterProfile(object obj)
        {
            if (obj is Tweet)
                DialogManager.ShowProfileMenu((obj as Tweet).TwitterProperty);
            if (obj is User)
                DialogManager.ShowProfileMenu(obj as User);
        }
        private bool CanFollowUser()
        {
            return CommonData.LOGGED_USER != null;
        }

        private void FollowUser(User user)
        {
            if (CommonData.IsFollowed(CommonData.LOGGED_USER, user) == false)
                _serviceCaller.Follow(CommonData.LOGGED_USER, user);
            else
                _serviceCaller.UnFollow(CommonData.LOGGED_USER, user);
            ServiceManager.RefreshCurrentUser();
        }

        private bool CanTwitterOpenProfile()
        {
            return true;
        }

        
    }
}

